/*
 * @Description: 引导线
 * @Date: 2022-04-15 14:31:21
 * @Author: yeming
 */
import { fromLonLat } from 'ol/proj.js'
import { Line, Line3d } from './line.js'
import { removeFeatureAndDeleteHashMap, removeAllFeatureAndDeleteHashMap } from '../common/removeFeature.js'

// 默认引导颜色（蓝色）
const DEFAULT_LINE = { 'fillColor': 'rgba(0, 153, 255, 1)', 'strokeWidth': 2, 'dash': [10, 6] }

class GuideLine {
  constructor(ol2d, ol3d, hashMap, vectorSource) {
    this.ol2d = ol2d
    this.ol3d = ol3d
    this.entities = ol3d.entities

    this.hashMap = hashMap
    this.vectorSource = vectorSource // uav相关矢量图层数据源
  }

  /**
   * @desc: 添加引导线
   * @param {Number} sessionId
   * @param {Array} latlng [[lon, lat, h], [lon, lat, h], ......, [lon, lat, h]]
   * @param {Object} style
   * @param {Boolean} rend3d
   */
  addGuideLine (sessionId, latlng, style, rend3d = false) {
    style = style && style != '' ? style : DEFAULT_LINE

    // 2d
    let line = new Line(this.ol2d, this.ol3d, this.vectorSource)
    line.addGuideLine(sessionId, latlng, style, rend3d)

    // 3d
    if (!rend3d) return false
    let positions = []
    latlng.forEach((item, index) => { positions[index] = new Cesium.Cartesian3.fromDegrees(item[0], item[1], item[2] || 80) })
    let line3d = new Line3d(this.entities, 'guideLine_' + sessionId, style, true, false)
    line3d.positions = positions

    this.hashMap.set(sessionId, { 'item2d': guideLine, 'item3d': guideLine3d })
  }

  /**
   * @desc: 更新引导线位置
   * @param {Number} sessionId
   * @param {Array} latlng [[lon, lat, h], [lon, lat, h], ......, [lon, lat, h]]
   */
  updateGuideLine (sessionId, latlng, style) {
    let currentLine = this.hashMap.get(sessionId)
    if (!currentLine) return false

    let positions = []
    let coordinates = []
    latlng.forEach(item => {
      coordinates.push(fromLonLat(item))
      positions.push(new Cesium.Cartesian3.fromDegrees(item[0], item[1], item[2] || 80))
    })
    currentLine.item2d.guideLine.setCoordinates(coordinates)
    currentLine.item3d.positions = positions
  }

  /**
   * @desc: 删除引导线
   * @param {Number} sessionId
   */
  delGuideLine (sessionId) {
    removeFeatureAndDeleteHashMap(sessionId, this.hashMap, this.vectorSource, this.entities)
  }

  /**
   * @desc: 删除所有引导线
   */
  delAllGuideLines () {
    removeAllFeatureAndDeleteHashMap(this.hashMap, this.vectorSource, this.entities)
  }
}

export default GuideLine